package com.shop.cloud.util;
//package com.common.util;
//
//import org.apache.commons.io.IOUtils;
//import org.apache.log4j.LogManager;
//import org.apache.log4j.Logger;
//
//import java.io.*;
//import java.nio.MappedByteBuffer;
//import java.nio.channels.FileChannel;
//import java.text.MessageFormat;
//import java.util.ArrayList;
//import java.util.Enumeration;
//import java.util.List;
//import java.util.zip.ZipEntry;
//import java.util.zip.ZipFile;
//import java.util.zip.ZipOutputStream;
//
//public final class FileUtils {
//
//	public static final String UTF_8_CHARSET = "UTF-8";
//	public static String familyNameDic = "/com/common/name2pingyin/data/familyName.txt";// 姓氏文件
//	public static String totalWordsDic = "/com/common/name2pingyin/data/words_pinyin_inorder.txt";// 所有单字文件
//
//	public static InputStream loadAssets(String familyNameFileName) {
//		return Thread.currentThread().getClass().getResourceAsStream(familyNameFileName);
//	}
//
//	public static List<String> readLine(File fullPath) {
//		List<String> builder = new ArrayList<String>();
//		BufferedReader reader = null;
//		try {
//			reader = new BufferedReader(new FileReader(fullPath));
//			String line = null;
//			while ((line = reader.readLine()) != null) {
//				builder.add(line);
//			}
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			if (reader != null) {
//				try {
//					reader.close();
//				} catch (IOException e) {
//				}
//			}
//		}
//		return builder;
//	}
//
//	public static String[] listFile(String dir) {
//		File file = new File(dir);
//		if (file.isDirectory() && file.canRead()) {
//			return file.list();
//		}
//		return null;
//	}
//
//	public static File[] listFileAs(String dir) {
//		File file = new File(Thread.currentThread().getClass().getResource(dir).getPath());
//		if (file.isDirectory() && file.canRead()) {
//			return file.listFiles();
//		}
//		return null;
//	}
//
//	public static final Logger LOG = LogManager.getLogger(FileUtils.class);
//	public static final String EOL = System.getProperty("line.separator");
//
//	private FileUtils() {
//	}
//
//	public static boolean deleteDir(final File dir) {
//		if (dir == null) {
//			return true;
//		}
//		if (dir.isDirectory()) {
//			final String[] children = dir.list();
//			for (final String child : children) {
//				final boolean success = deleteDir(new File(dir, child));
//				if (!success) {
//					return false;
//				}
//			}
//		}
//		return dir.delete();
//	}
//
//	public static void fastDeleteDir(final File dir) {
//		final String name = dir.getName() + "-" + System.currentTimeMillis();
//		final File parent = dir.getParentFile();
//		final File toBeDeleted = new File(parent, name);
//
//		dir.renameTo(toBeDeleted);
//		final boolean success = deleteDir(toBeDeleted);
//		if (!success) {
//			LOG.error("Failed to remove dir " + toBeDeleted);
//		}
//	}
//
//	@SuppressWarnings("resource")
//	public static void copyLargeFile(final File src, final File dest) throws IOException {
//		FileChannel in = null, out = null;
//		try {
//			in = new FileInputStream(src).getChannel();
//			out = new FileOutputStream(dest).getChannel();
//			final long size = in.size();
//			final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
//			out.write(buf);
//		} finally {
//			if (in != null) {
//				in.close();
//			}
//			if (out != null) {
//				out.close();
//			}
//		}
//	}
//
//	public static void copySmallFile(final File src, final File dest) throws IOException {
//		final int bufferSize = 1024;
//		final InputStream in = new FileInputStream(src);
//		final OutputStream out = new FileOutputStream(dest);
//		final byte[] buf = new byte[bufferSize];
//		int len;
//		while ((len = in.read(buf)) > 0) {
//			out.write(buf, 0, len);
//		}
//		in.close();
//		out.close();
//	}
//
//	public static StringBuffer readFile(final String fileName) throws IOException {
//		return readString(new FileReader(fileName));
//	}
//
//	public static StringBuffer readFile(final File file) throws IOException {
//		return readString(new FileReader(file));
//	}
//
//	public static StringBuffer readString(final InputStream in) throws IOException {
//		return readString(new InputStreamReader(in));
//	}
//
//	public static StringBuffer readString(final Reader reader) {
//		final StringBuffer actual = new StringBuffer(1024);
//		BufferedReader mReader = new BufferedReader(reader);
//		String s;
//		try {
//			while (null != (s = mReader.readLine())) {
//				actual.append(s).append("\n");
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			IOUtilities.close(mReader);
//		}
//		return actual;
//	}
//
//	public static List<String> readLines(final InputStream is) throws IOException {
//		return readLines(is, -1);
//	}
//
//	public static List<String> readLines(final String fileName) throws IOException {
//		return readLines(new FileInputStream(new File(fileName)), -1);
//	}
//
//	public static List<String> readLines(final String fileName, final int max) throws IOException {
//		return readLines(new FileInputStream(new File(fileName)), max);
//	}
//
//	public static List<String> readLines(final InputStream is, final int max) throws IOException {
//		final List<String> lines = new ArrayList<String>();
//		final Reader reader = new InputStreamReader(is);
//		final LineNumberReader r = new LineNumberReader(reader);
//		String s;
//		int count = 0;
//		while (null != (s = r.readLine())) {
//			lines.add(s);
//			count++;
//			if (count == max) {
//				break;
//			}
//		}
//		r.close();
//		return lines;
//	}
//
//	public interface KeyFunction {
//		int eval(int key);
//	}
//
//	public static List<String> readLines(final String fileName, final int key, final KeyFunction keyFunction, final int keyPosition, final String delimiter)
//			throws IOException {
//		final List<String> lines = new ArrayList<String>();
//		final Reader reader = new FileReader(new File(fileName));
//		final LineNumberReader r = new LineNumberReader(reader);
//		String s;
//		int count = 0;
//		int numExceptions = 0;
//		while (null != (s = r.readLine())) {
//			final String[] splits = s.split(delimiter);
//			try {
//				final int functKey = Integer.parseInt(splits[keyPosition].trim());
//				if (keyPosition < splits.length && keyFunction.eval(functKey) == key) {
//					lines.add(s.trim());
//					count++;
//				}
//			} catch (final NumberFormatException ex) {
//				numExceptions++;
//			}
//		}
//		r.close();
//		LOG.info("Accepted " + count + " lines. Exceptions in " + numExceptions + " lines.");
//		return lines;
//	}
//
//	public static void consumeLines(final InputStream is, final FileConsumer consumer) throws IOException {
//		final LineNumberReader r = new LineNumberReader(new InputStreamReader(is));
//		consumeWithReader(r, consumer);
//	}
//
//	public static void consumeLines(final String fileName, final FileConsumer consumer) throws IOException {
//		final LineNumberReader r = new LineNumberReader(new FileReader(new File(fileName)));
//		consumeWithReader(r, consumer);
//	}
//
//	private static void consumeWithReader(final LineNumberReader r, final FileConsumer consumer) throws IOException {
//		String s;
//		while (null != (s = r.readLine())) {
//			consumer.consume(s);
//		}
//		r.close();
//	}
//
//	public interface FileConsumer {
//		void consume(String line);
//	}
//
//	public static List<File> getFilesRecursively(final String topLevelDirName) {
//		final List<File> fileList = new ArrayList<File>();
//		final File topLevelDir = new File(topLevelDirName);
//		getFilesRecursively0(topLevelDir, fileList);
//		return fileList;
//	}
//
//	private static void getFilesRecursively0(final File dir, final List<File> fileList) {
//		for (final File file : dir.listFiles()) {
//			if (file.isDirectory()) {
//				getFilesRecursively0(file, fileList);
//			} else {
//				if (!file.getName().startsWith(".") && !file.getName().endsWith(".xlsx")) {
//					fileList.add(file);
//				}
//			}
//		}
//	}
//
//	public static List<String> readZipFile(final String sourceZipFileName) throws IOException {
//		final int reportFreq = 250;
//		final List<String> resultingItems = new ArrayList<String>();
//		final File sourceZipFile = new File(sourceZipFileName);
//		final ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
//		final Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries();
//		int entriesCount = 1;
//		while (zipFileEntries.hasMoreElements()) {
//			final ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
//			if (!entry.isDirectory()) {
//				final BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
//				final String contents = FileUtils.readString(is).toString();
//				resultingItems.add(contents);
//				IOUtils.closeQuietly(is);
//			}
//			if (entriesCount % reportFreq == 0) {
//				LOG.info("Processed " + entriesCount + " entries in the " + sourceZipFileName + " zip file.");
//			}
//			entriesCount++;
//		}
//		LOG.info("Done. Processed all " + entriesCount + " entries in the " + sourceZipFileName + " zip file.");
//		zipFile.close();
//		return resultingItems;
//	}
//
//	public static void writeToZipFile(final String dirToZip, final String zipFileName) throws IOException {
//		writeToZipFile(dirToZip, zipFileName, null);
//	}
//
//	public static void writeToZipFile(final String dirToZip, final String zipFileName, final FilenameFilter filter) throws IOException {
//		final File outputDirFile = new File(dirToZip);
//		try {
//			final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFileName));
//			zos.setLevel(9);
//			zos.setMethod(ZipOutputStream.DEFLATED);
//			final File[] files = filter == null ? outputDirFile.listFiles() : outputDirFile.listFiles(filter);
//			for (final File file : files) {
//				final ZipEntry ze = new ZipEntry(file.getName());
//				ze.setTime(file.lastModified());
//				FileInputStream fis = null;
//				try {
//					final int fileLength = (int) file.length();
//					fis = new FileInputStream(file);
//					final byte[] wholeFile = new byte[fileLength];
//					final int bytesRead = fis.read(wholeFile, 0, fileLength);
//					if (bytesRead != fileLength) {
//						LOG.error("Error in processing " + file);
//					}
//					zos.putNextEntry(ze);
//					zos.write(wholeFile, 0, fileLength);
//					zos.closeEntry();
//				} finally {
//					if (fis != null) {
//						fis.close();
//					}
//				}
//			}
//			zos.flush();
//			zos.finish();
//			zos.close();
//		} catch (final FileNotFoundException e) {
//			LOG.error("Error in zipping", e);
//		} catch (final IOException e) {
//			LOG.error("Error in zipping", e);
//		}
//		org.apache.commons.io.FileUtils.deleteDirectory(outputDirFile);
//	}
//
//	public static class XMLFilenameFilter implements FilenameFilter {
//		@Override
//		public boolean accept(final File dir, final String name) {
//			return name.endsWith("xml");
//		}
//	}
//
//	public static class TxtFilenameFilter implements FilenameFilter {
//		@Override
//		public boolean accept(final File dir, final String name) {
//			return name.endsWith("txt");
//		}
//	}
//
//	public static void writeTextFile(final String fileName, final String text) throws IOException {
//		FileWriter writer = null;
//		try {
//			writer = new FileWriter(fileName);
//			writer.append(text);
//			writer.flush();
//		} finally {
//			if (writer != null) {
//				writer.close();
//			}
//		}
//	}
//
//	public static void appendToTextFile(final String fileName, final String text) throws IOException {
//		FileWriter writer = null;
//		try {
//			writer = new FileWriter(fileName, true);
//			writer.append(text);
//			writer.flush();
//		} finally {
//			if (writer != null) {
//				writer.close();
//			}
//		}
//	}
//
//	public static void delete(File file) throws IOException {
//		if (file.getAbsolutePath().length() < 5) {
//			throw new IllegalArgumentException(MessageFormat.format("Path [{0}] is too short, not deleting", file.getAbsolutePath()));
//		}
//		if (file.exists()) {
//			if (file.isDirectory()) {
//				File[] children = file.listFiles();
//				if (children != null) {
//					for (File child : children) {
//						delete(child);
//					}
//				}
//			}
//			if (!file.delete()) {
//				throw new RuntimeException(MessageFormat.format("Could not delete path [{0}]", file.getAbsolutePath()));
//			}
//		}
//	}
//
//	public static void copy(InputStream is, OutputStream os) throws IOException {
//		Assert.notNull(is, "is");
//		Assert.notNull(os, "os");
//		copy(is, os, 0, -1);
//	}
//
//	public static void copy(InputStream is, OutputStream os, long offset, long len) throws IOException {
//		Assert.notNull(is, "is");
//		Assert.notNull(os, "os");
//		byte[] buffer = new byte[1024];
//		long skipped = is.skip(offset);
//		if (skipped == offset) {
//			if (len == -1) {
//				int read = is.read(buffer);
//				while (read > -1) {
//					os.write(buffer, 0, read);
//					read = is.read(buffer);
//				}
//				is.close();
//			} else {
//				long count = 0;
//				int read = is.read(buffer);
//				while (read > -1 && count < len) {
//					count += read;
//					if (count < len) {
//						os.write(buffer, 0, read);
//						read = is.read(buffer);
//					} else if (count == len) {
//						os.write(buffer, 0, read);
//					} else {
//						int leftToWrite = read - (int) (count - len);
//						os.write(buffer, 0, leftToWrite);
//					}
//				}
//			}
//			os.flush();
//		} else {
//			throw new IOException(MessageFormat.format("InputStream ended before offset [{0}]", offset));
//		}
//	}
//
//	public static void copy(Reader reader, Writer writer) throws IOException {
//		Assert.notNull(reader, "reader");
//		Assert.notNull(writer, "writer");
//		copy(reader, writer, 0, -1);
//	}
//
//	public static void copy(Reader reader, Writer writer, long offset, long len) throws IOException {
//		Assert.notNull(reader, "reader");
//		Assert.notNull(writer, "writer");
//		char[] buffer = new char[1024];
//		long skipped = reader.skip(offset);
//		if (skipped == offset) {
//			if (len == -1) {
//				int read = reader.read(buffer);
//				while (read > -1) {
//					writer.write(buffer, 0, read);
//					read = reader.read(buffer);
//				}
//				reader.close();
//			} else {
//				long count = 0;
//				int read = reader.read(buffer);
//				while (read > -1 && count < len) {
//					count += read;
//					if (count < len) {
//						writer.write(buffer, 0, read);
//						read = reader.read(buffer);
//					} else if (count == len) {
//						writer.write(buffer, 0, read);
//					} else {
//						int leftToWrite = read - (int) (count - len);
//						writer.write(buffer, 0, leftToWrite);
//					}
//				}
//			}
//			writer.flush();
//		} else {
//			throw new IOException(MessageFormat.format("Reader ended before offset [{0}]", offset));
//		}
//	}
//
//	public static String toString(Reader reader) throws IOException {
//		Assert.notNull(reader, "reader");
//		StringWriter writer = new StringWriter();
//		copy(reader, writer);
//		return writer.toString();
//	}
//
//	public static long zipDir(File dir, String relativePath, ZipOutputStream zos) throws IOException {
//		Assert.notNull(dir, "dir");
//		Assert.notNull(relativePath, "relativePath");
//		Assert.notNull(zos, "zos");
//		return zipDir(dir, relativePath, zos, true);
//	}
//
//	private static long zipDir(File dir, String relativePath, ZipOutputStream zos, boolean start) throws IOException {
//		String[] dirList = dir.list();
//		long size = 0;
//		for (String aDirList : dirList) {
//			File f = new File(dir, aDirList);
//			if (!f.isHidden()) {
//				if (f.isDirectory()) {
//					if (!start) {
//						ZipEntry dirEntry = new ZipEntry(relativePath + f.getName() + "/");
//						size += dirEntry.getSize();
//						zos.putNextEntry(dirEntry);
//						zos.closeEntry();
//					}
//					String filePath = f.getPath();
//					File file = new File(filePath);
//					zipDir(file, relativePath + f.getName() + "/", zos, false);
//				} else {
//					ZipEntry anEntry = new ZipEntry(relativePath + f.getName());
//					size += anEntry.getSize();
//					zos.putNextEntry(anEntry);
//					InputStream is = new FileInputStream(f);
//					byte[] arr = new byte[4096];
//					int read = is.read(arr);
//					while (read > -1) {
//						zos.write(arr, 0, read);
//						read = is.read(arr);
//					}
//					is.close();
//					zos.closeEntry();
//				}
//			}
//		}
//		IOUtilities.close(zos);
//		return size;
//	}
//}
